#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/mii.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <netinet/in.h>
#include <unistd.h> // for close
#define reteck(ret) \
        if(ret < 0){    \
                printf("%m! \"%s\" : line: %d\n", __func__, __LINE__);   \
                goto lab;   \
        }
#define help() \
        printf("mdio:\n");                  \
printf("read operation: mdio reg_addr\n");          \
printf("write operation: mdio reg_addr value\n");    \
printf("For example:\n");            \
printf("mdio eth0 3 1\n");             \
printf("mdio eth0 3 0 0x12\n\n");      \
exit(0);
int sockfd;

#define MMD_ACR (0x0d)
#define MMD_ADR (0x0e)
#define DATA_FLAG (0x1 << 14)
int main(int argc, char *argv[]){

	uint16_t mmd;
        if(argc == 1 || !strcmp(argv[1], "-h")){
                help();
        }

        struct mii_ioctl_data *mii = NULL;
        struct ifreq ifr;
        int ret;

        memset(&ifr, 0, sizeof(ifr));
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);

        sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
        reteck(sockfd);

        //get phy address in smi bus
        ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);
        reteck(ret);

        mii = (struct mii_ioctl_data*)&ifr.ifr_data;

	// device addr is argv[2]
	// address is argv 3
        if(argc == 4){
		
                mmd     = (uint16_t)strtoul(argv[2], NULL, 0);
		// 1 write device addr to acr
                mii->reg_num    = MMD_ACR;
                mii->val_in     = (uint16_t)strtoul(argv[2], NULL, 0);
		printf("write %x --> %x\n", mii->val_in, mii->reg_num);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);
		// 2 write address to adr 
                mii->reg_num    = MMD_ADR;
                mii->val_in     = (uint16_t)strtoul(argv[3], NULL, 0);
		printf("write %x --> %x\n", mii->val_in, mii->reg_num);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);
		// 3 write data flags to acr
                mii->reg_num    = MMD_ACR;
                mii->val_in     = (uint16_t)strtoul(argv[2], NULL, 0) | DATA_FLAG;
		printf("write %x --> %x\n", mii->val_in, mii->reg_num);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);

		// 4 read data from adr
                mii->reg_num    = MMD_ADR;
                ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
                reteck(ret);
		printf("read %x --> %x\n", mii->val_out, mii->reg_num);

                printf("read mmd %d addr: 0x%x  reg: 0x%x   value : 0x%x\n\n", mmd, mii->phy_id, mii->reg_num, mii->val_out);
        }else if(argc == 5){
                mmd     = (uint16_t)strtoul(argv[2], NULL, 0);
		// 1 write device addr to acr
                mii->reg_num    = MMD_ACR;
                mii->val_in     = (uint16_t)strtoul(argv[2], NULL, 0);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);
		// 2 write address to adr 
                mii->reg_num    = MMD_ADR;
                mii->val_in     = (uint16_t)strtoul(argv[3], NULL, 0);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);
		// 3 write data flags to acr
                mii->reg_num    = MMD_ACR;
                mii->val_in     = (uint16_t)strtoul(argv[2], NULL, 0) | DATA_FLAG;
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);

		// 4 write data to adr
                mii->reg_num    = MMD_ADR;
                mii->val_in     = (uint16_t)strtoul(argv[4], NULL, 0);
                ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
                reteck(ret);


                printf("write %d phy addr: 0x%x  reg: 0x%x  value : 0x%x\n\n", mmd, mii->phy_id, mii->reg_num, mii->val_in);
        }
lab:

        close(sockfd);
        return 0;
}

